﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Resources;
using Cesave.Domain.Core;
using System.Data;
using Cesave.Domain.PermisosModule.Contracts.CultivosFechas;
using Cesave.Domain.PermisosModule.Contracts.CiclosFechas;

namespace Cesave.Domain.PermisosModule.Contracts.Cultivos
{
	/// <summary>
	/// 
	/// </summary>
	public class CultivoService
		: ICultivoService
	{

		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private ICultivoRepository _cultivoRepository;

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cultivoRepository"></param>
		public CultivoService(ICultivoRepository cultivoRepository)
		{
			if (cultivoRepository == null)
				throw new ArgumentNullException(Messages.exception_DependenciesAreNotInitialized, "cultivoRepository");
			_cultivoRepository = cultivoRepository;
		}

		#endregion

		#region ICultivoService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cultivo"></param>
		public void AddCultivo(Entities.Cultivo cultivo)
		{
			if (cultivo == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "cultivo");
			IUnitOfWork unitOfWork = _cultivoRepository.StoreContext;
			_cultivoRepository.Add(cultivo);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cultivo"></param>
		public void ChangeCultivo(Entities.Cultivo cultivo)
		{
			if (cultivo == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "cultivo");
			IUnitOfWork unitOfWork = _cultivoRepository.StoreContext;
			_cultivoRepository.Modify(cultivo);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public List<Entities.Cultivo> FindAllCultivos()
		{
			return _cultivoRepository.GetAll().OrderBy(c => c.Nombre).ToList();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cicloGuid"></param>
		/// <param name="juntaGuid"></param>
		/// <param name="fromDate"></param>
		/// <param name="toDate"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Entities.Cultivo> FindCultivosForExpedicion(Guid cicloGuid, Guid juntaGuid, DateTime fromDate, DateTime toDate, bool status = true)
		{
			return _cultivoRepository.FindCultivosForExpedicion(
				new CultivosByStatusSpecification(status),
				new CiclosFechasByCicloSpecification(cicloGuid),
				new CultivosFechasByJuntaSpecification(juntaGuid),
				new CiclosFechasByDateRangeSpecification(fromDate, toDate)
			).ToList();
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			if (_cultivoRepository != null && _cultivoRepository.StoreContext != null)
				_cultivoRepository.StoreContext.Dispose();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Entities.Cultivo> FindCultivosByStatus(bool status = true)
		{
			CultivosByStatusSpecification specification = new CultivosByStatusSpecification(status);
			return _cultivoRepository.FindCultivosByStatus(specification).ToList();
		}

		#endregion
	}
}
